package netty.udp;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import netty.common.serializer.UdpDecoder;
import netty.common.serializer.UdpEncoder;

import java.util.concurrent.CountDownLatch;

/**
 * 	 <B>说	明<B/>:UdpServer
 */
public class UdpServer {

    public void start() throws Exception{
        try {
            NioEventLoopGroup acceptGroup = new NioEventLoopGroup();

            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(acceptGroup)
                    .channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_BROADCAST, false)
                    .option(ChannelOption.SO_SNDBUF, 300 * 1024 * 1024)
                    .option(ChannelOption.SO_RCVBUF, 300 * 1024 * 1024)
                    .handler(new ChannelInitializer<NioDatagramChannel>() {
                        @Override
                        protected void initChannel(NioDatagramChannel ch)
                                throws Exception {
                            ch.pipeline().addLast(new UdpEncoder());
                            ch.pipeline().addLast(new UdpDecoder());
                            ch.pipeline().addLast(new ServerHandler());
                        }
                    });
            bootstrap.bind("0.0.0.0", 7775).sync();

            System.out.println("UdpServerHandler start success at port 7775");
//            channel.closeFuture().await();
        } finally {
//            acceptGroup.shutdownGracefully();
        }

        new CountDownLatch(1).await();
    }


}